<?php

namespace Modules\Edu\Http\Controllers\Front;

use App\Models\LiveAdmin;
use App\User;
use GatewayClient\Gateway;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;


class ChatController extends Controller
{
    protected $masses = [];

    public function __construct()
    {
        Gateway::$registerAddress = '127.0.0.1:1238';
    }

    public function init(Request $request)
    {
        //登录验证
        if (!Auth::check()) {
            return;
        }

        //Info获得
        $uid = user('id');
        $client_id = $request->input('client_id');
        $live_id = $request->input('live_id');
        //客户端和用户ID绑定
        Gateway::bindUid($client_id, $uid);
        //分组
        //如果不分组,会所有房间都发送消息
        Gateway::joinGroup($client_id, $live_id);

        if ($this->checkUserKickOut($live_id)) {

            $this->closeClient($uid);

            return;
        }
        //直播间组内广播消息
        $this->sendToGroupAll($live_id, '进入直播间');
        //单独发系统公告
        $this->systemSend($uid, '文明上网');

    }

    public function checkUserSpeek()
    {
        $liveAdmin = LiveAdmin::CurrentUser()->first();
        return $liveAdmin ? $liveAdmin->isspeek : false;
    }

    public function checkUserKickOut($live_id)
    {
        $liveAdmin = LiveAdmin::CurrentUser($live_id)->first();

        return $liveAdmin ? $liveAdmin->iskickout : false;
    }

    public function send(Request $request)
    {
        if (!Auth::check()) {
            return;
        }

        $content = $request->input('content');
        $from = user('id');
        $to = $request->input('toId');

        //如果禁言单独发系统消息
        if ($this->checkUserSpeek()) {
            $this->systemSend($from, '你已被管理员禁言');
            return;
        }

        if ($to) {
            $this->sendToUid($from, $to, $content);
        } else {
            $this->sendToGroupAll($request->input('live_id'), $content);
        }
    }

    public function systemSend($uid, $content, $type = null, $route = null)
    {
        Gateway::sendToUid($uid, json_encode([
            'type' => $type,
            'route' => $route,
            'from' => [
                'id' => 0,
                'nickname' => '系统消息',
            ],
            'to' => [

            ],
            'content' => $content
        ]));
    }

//    @param $from  int id
//    @param $to    int id


    public function getGroupUserInfo($live_id)
    {

        $uidList = Gateway::getUidListByGroup($live_id);

        //只保留id和name属性
        $res = User::find($uidList)->toArray();
        $res = array_map(function ($v) {

            return array_filter($v, function ($k) {
                return $k == 'id' || $k == 'name';
            }, ARRAY_FILTER_USE_KEY);
        }, $res);
        return $res;
    }

    //踢出
    public function closeClient($uid, $route = null, $message = '管理员将你踢出房间')
    {
        if (is_null($uid)) {
            return;
        }
        //转到地址
        if (is_null($route)) {
            $route = route('edu.front.live.index');
        }
        //获取uid的所有客户端
        $client_ids = Gateway::getClientIdByUid($uid);
        //转至页面message消息
        request()->session()->flash('danger', $message);
        $this->systemSend($uid, $message, 'kickout', $route);

        foreach ($client_ids as $client_id) {
            Gateway::closeClient($client_id);
        }

    }

    //通过Uid获取ClientId
    public function getClientId($Uid)
    {
        return var_export(Gateway::getClientIdByUid($Uid));
    }

    //为组内所有观众发送消息
    public function sendToGroupAll($live_id, $content,$nickname='')
    {
        $nickname=$nickname=='' ? user('name'): $nickname;
        $message = json_encode([
            'from' => [
                'id' => user('id'),
                'nickname' => $nickname,
            ],
            'to' => [],
            'content' => $content
        ]);
        Gateway::sendToGroup($live_id, $message);
    }

    //为Uid发送消息
    public function sendToUid($from, $to, $content)
    {
        $fromUser = app(User::class)->find($from);
        $toUser = app(User::class)->find($to);
        //发给目标用户
        Gateway::sendToUid($to, json_encode([
            'from' => [
                'id' => $from,
                'nickname' => $fromUser->nickname,
            ],
            'to' => [
                'id' => $to,
                'nickname' => $toUser->nickname,
            ],
            'content' => $content
        ]));
        //回传给自己
        Gateway::sendToUid($from, json_encode([
            'from' => [
                'id' => $from,
                'nickname' => $fromUser->nickname,
            ],
            'to' => [
                'id' => $to,
                'nickname' => $toUser->nickname,
            ],
            'content' => $content
        ]));
    }
}
